﻿Public Class frm_main
  Dim colorToBeChanged As Integer
  Dim verlauf1, verlauf2 As Color

  Dim gradientAuto As Boolean, gradientStyle As Integer
  Dim ctrlShiftActive As Boolean

  Dim colorFormat1 = "", colorFormat2 As String = "", htmlUCase As Boolean


  Private Sub frm_main_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    glob.saveFormPos(Me)
    glob.saveTuttiFrutti(Me)
  End Sub

  Private Sub frm_main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    colorToBeChanged = 1
    glob.readFormPos(Me)
    glob.readTuttiFrutti(Me)
    mwRegisterSelf()
    readSettings()
  End Sub
  Function replaceColorFormat(ByVal fmt As String, ByVal col As Color)
    fmt = fmt.Replace("{R", "{0").Replace("{G", "{1").Replace("{B", "{2")
    Return String.Format(fmt, col.R, col.G, col.B, col.A)
  End Function
  Sub readSettings()
    gradientAuto = glob.para("frm_options__Gradientmode", "man") = "auto"
    gradientStyle = glob.para("GradientStyle", "0")
    ColorPicker1.FlyOut = If(glob.para("frm_options__autoFlyout", "FALSE") = "TRUE", ColorPicker.eFlyOut.Auto, ColorPicker.eFlyOut.Click)
    tmr_getScreenPixelColor.Enabled = glob.para("frm_options__ctrlShiftActive", "TRUE") = "TRUE"
    colorFormat1 = glob.para("frm_options__CustomFormat1")
    colorFormat2 = glob.para("frm_options__CustomFormat2")
    htmlUCase = glob.para("frm_options__htmlUCase", "FALSE") = "TRUE"
  End Sub

  Private Sub ColorPicker1_ColorChanging(ByVal sender As Object) Handles ColorPicker1.ColorChanging
    Dim c As Color = ColorPicker1.Value
    ' btnHTML.Text = ColorTranslator.ToHtml(c)
    If htmlUCase Then
      btnHTML.Text = UCase("#" + c.R.ToString("x2") + c.G.ToString("x2") + c.B.ToString("x2"))
    Else
      btnHTML.Text = LCase("#" + c.R.ToString("x2") + c.G.ToString("x2") + c.B.ToString("x2"))
    End If
    btnVB_RGB.Text = replaceColorFormat(colorFormat1, c)
    btn_rgb.Text = replaceColorFormat(colorFormat2, c)

    'btnVB_RGB.Text = c.R & "; " & c.G & "; " & c.B
    'btn_rgb.Text = "rgb (" & c.R & ", " & c.G & ", " & c.B & ")"
    btnOle.Text = "" & ColorTranslator.ToOle(c)
    'btn_win32.Text = "win32: " & ColorTranslator.ToWin32(c)
    Label1.BackColor = c : Label2.BackColor = c
    If colorToBeChanged = 1 And gradientAuto Then verlauf1 = c : pbVerlauf.Invalidate()
    If colorToBeChanged = 2 And gradientAuto Then verlauf2 = c : pbVerlauf.Invalidate()

  End Sub

  Private Sub ColorPicker1_ColorPicked(ByVal sender As System.Object) Handles ColorPicker1.ColorPicked
    
  End Sub

  Private Sub btn_toggleView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    ColorPicker1.HideRGB = Not ColorPicker1.HideRGB
    Me.Width = ColorPicker1.Width + 8

  End Sub

  Private Sub tmr_getScreenPixelColor_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmr_getScreenPixelColor.Tick
    If isShiftControl() Then
      ColorPicker1.Value = getScreenPixelColorFromCursor()

    End If
  End Sub

  Private Sub pbVerlauf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbVerlauf.Click

  End Sub

  Private Sub pbVerlauf_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbVerlauf.MouseClick
    Dim leftSide As Boolean = e.X < pbVerlauf.Width / 2
    If gradientAuto Then
      If leftSide Then
        colorToBeChanged = 1
        ColorPicker1.Value = verlauf1
      Else
        colorToBeChanged = 2
        ColorPicker1.Value = verlauf2
      End If


    Else
      If leftSide Then
        If e.Button = Windows.Forms.MouseButtons.Left Then
          verlauf1 = ColorPicker1.Value
        Else
          ColorPicker1.Value = verlauf1
        End If
      Else
        If e.Button = Windows.Forms.MouseButtons.Left Then
          verlauf2 = ColorPicker1.Value
        Else
          ColorPicker1.Value = verlauf2
        End If
      End If

    End If

    pbVerlauf.Invalidate()
  End Sub

  Private Sub pbVerlauf_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pbVerlauf.MouseMove
    If e.X < pbVerlauf.Width / 2 Then
      pbVerlauf.Cursor = Cursors.PanWest
    Else
      pbVerlauf.Cursor = Cursors.PanEast
    End If
  End Sub

  Private Sub pbVerlauf_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pbVerlauf.Paint
    Dim rect As New Rectangle(0, 0, pbVerlauf.Width, pbVerlauf.Height)
    Dim br As New Drawing2D.LinearGradientBrush(rect, verlauf1, verlauf2, DirectCast(gradientStyle, Drawing2D.LinearGradientMode))
    e.Graphics.FillRectangle(br, rect)
    Dim xPos As Integer
    If colorToBeChanged = 1 And gradientAuto Then
      e.Graphics.DrawPie(Pens.Black, 10, 10, 14, 14, 30, 300)
    End If
    If colorToBeChanged = 2 And gradientAuto Then
      e.Graphics.DrawPie(Pens.Black, pbVerlauf.Width - 24, 10, 14, 14, 210, 300)
    End If
    
  End Sub


  Private Sub btnOle_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVB_RGB.Click, btnOle.Click, btnHTML.Click, btn_rgb.Click
    Clipboard.Clear()
    Clipboard.SetText(sender.Text)
  End Sub

  Private Sub checkTopmost_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles checkTopmost.CheckedChanged
    TopMost = checkTopmost.Checked
  End Sub

  Private Sub rbAuto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    pbVerlauf.Invalidate()
  End Sub

  Private Sub btnConfig_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConfig.Click
    frm_options.ShowDialog()
    readSettings()
    pbVerlauf.Invalidate()

  End Sub

  Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
    AboutBox1.ShowDialog()

  End Sub
End Class